package corm

import (
	"github.com/sirupsen/logrus"
	"time"
)

type Config struct {
	Driver      DBDriver
	Dsn         string
	TablePrefix string

	MaxIdleConns    int
	MaxOpenConns    int
	ConnMaxIdleTime time.Duration

	Logger *logrus.Logger // 集成主项目的日志实例

	//数据表引擎 InnoDB(default) MyISAM
	Engine string

	//AutoMigrate bool // 新增字段，控制自动迁移
}

func (this *Config) HasDsn() bool {
	return this.Dsn != ""
}

func (this *Config) IsMysql() bool {
	return this.Driver == DBDriverMySQL
}

func (this *Config) IsSqlite() bool {
	return this.Driver == DBDriverSqlite
}

func (this *Config) IsPostgres() bool {
	return this.Driver == DBDriverPostgres
}

// 合并配置
func (this *Config) Merge(newConfig *Config) *Config {
	if newConfig.Driver != "" {
		this.Driver = newConfig.Driver
	}
	if newConfig.Dsn != "" {
		this.Dsn = newConfig.Dsn
	}
	if newConfig.TablePrefix != "" {
		this.TablePrefix = newConfig.TablePrefix
	}
	if newConfig.MaxIdleConns > 0 {
		this.MaxIdleConns = newConfig.MaxIdleConns
	}
	if newConfig.MaxOpenConns > 0 {
		this.MaxOpenConns = newConfig.MaxOpenConns
	}
	if newConfig.ConnMaxIdleTime > 0 {
		this.ConnMaxIdleTime = newConfig.ConnMaxIdleTime
	}
	if newConfig.Logger != nil {
		this.Logger = newConfig.Logger
	}
	if newConfig.Engine != "" {
		this.Engine = newConfig.Engine
	}
	return this
}

func DefaultConfig() *Config {
	return &Config{
		TablePrefix:     "z_",
		MaxIdleConns:    10,
		MaxOpenConns:    100,
		ConnMaxIdleTime: time.Hour,

		Engine: "InnoDB",

		//Logger             *logrus.Logger // 集成主项目的日志实例
		//AutoMigrate:     false,
	}
}
